package com.erdos.upc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.erdos.common.service.BaseCRUDService;
import com.erdos.upc.dao.PrivilegeDao;
import com.erdos.upc.entity.Privilege;
import com.erdos.upc.service.PrivilegeService;


@SuppressWarnings("serial")
public class PrivilegeServiceImpl extends BaseCRUDService<Privilege> implements PrivilegeService {
	
	private PrivilegeDao privilegeDao;
	
	/**
	 * 完成权限的创建
	 */
	
	public Long insert(Privilege privilege) {
		return super.insert(privilege);
	}
	
	/**
	 * 更新权限
	 */
	
	public void update(Privilege privilege) {
		super.update(privilege);
	}
	
	/**
	 * 删除权限
	 */
	
	public int delete(Privilege privilege) {
		getPrivilegeDao().deletePrivilegeRole(privilege.getSid());
		return getPrivilegeDao().delete(privilege);
	}

	/**
	 * 对权限名称进行校验
	 * @param sid
	 * @param name
	 * @return
	 */
	
	public boolean checkName(String sid,String name) {
		boolean flag;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		Privilege privilege = getPrivilegeDao().findUniqueByParams(params);
		
		if (StringUtils.isEmpty(sid)) {
			flag=( privilege == null ? false : true);
		} else {
			if (privilege==null)
				flag= false;
			else if (sid.equals(privilege.getSid().toString())) {
				flag= false;
			} else {
				flag= true;
			}
		}
		return flag;
	}
	
	/**
     * 对权限编码进行校验
     * @param sid
     * @param code
     * @return
     */
	
	public boolean checkCode(String sid,String code) {
		boolean flag;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("code", code);
		Privilege privilege = getPrivilegeDao().findUniqueByParams(params);
		if (StringUtils.isEmpty(sid)) {
			flag=( privilege == null ? false : true);
		} else {
			if (privilege==null)
				flag= false;
			else if (sid.equals(privilege.getSid().toString())) {
				flag= false;
			} else {
				flag= true;
			}
		}
		return flag;
	}
	
	/**
	 * 对权限URL进行校验
	 * @param sid
	 * @param url
	 * @return
	 */
	
	public boolean checkUrl(String sid,String url) {
		boolean flag;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("url", url);
		Privilege privilege = getPrivilegeDao().findUniqueByParams(params);
		if (StringUtils.isEmpty(sid)) {
			flag=( privilege == null ? false : true);
		} else {
			if (privilege==null)
				flag= false;
			else if (sid.equals(privilege.getSid().toString())) {
				flag= false;
			} else {
				flag= true;
			}
		}
		return flag;
	}

	public PrivilegeDao getPrivilegeDao() {
		return privilegeDao;
	}

	public void setPrivilegeDao(PrivilegeDao privilegeDao) {
		this.privilegeDao = privilegeDao;
	}

	public List<Privilege> findAllPrivilegeByUserSid(Long userSid) {
		return privilegeDao.findAllPrivilegeByUserSid(userSid);
	}

//    public void deleteRoleR2Privilege(Long roleSid,Long privilegeSid) {
//        privilegeDao.deleteRoleR2Privilege(roleSid,privilegeSid);
//        
//    }

    public Privilege findByRptId(String rptId) {
        return privilegeDao.findByRptId(rptId);
    }

    public void deleteRoleR2Privilege(Long privilegeSid) {
      privilegeDao.deleteRoleR2Privilege( privilegeSid);
        
    }

//    public  List<Privilege> findByRptId(String rptId) {
//        return privilegeDao.findByRptId(rptId);
//    }

}
